package com.company.HashTable;

import java.util.Hashtable;
import java.util.Scanner;

/**
 * @author VX5
 * @Title: MJC
 * @ProjectName DataStructure
 * @Description: TODO
 * @date ${DAT}21:28
 */
public class HashDemo {
    public static void main(String[] args) {
        EmpLinkedList.HashTable hashTable = new EmpLinkedList.HashTable(7);

        String key = "";
        Scanner scanner = new Scanner(System.in);
        while (true){
            System.out.println("add: 添加雇员");
            System.out.println("");
        }
    }
}

//表示一个雇员
class Emp{
    private int id;
    private String name;
    public Emp next;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "Emp{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", next=" + next +
                '}';
    }
}

//创建EmpLinkedList，表示链表
class EmpLinkedList{
    private Emp head;//默认null

    public void add(Emp emp){
        if (head == null){
            head = emp;
            return;
        }
        Emp curEmp = head;
        while (true){
            if (curEmp.next == null){
                break;
            }
            curEmp = curEmp.next;
        }
        //退出时直接将Emp加入链表
        curEmp.next = emp;
    }

    public void list(){
        if (head == null){
            System.out.println("当前链表为空");
            return;
        }
        System.out.println("当前链表信息为：");
        Emp curEmp = head;
        while (true){
            if (curEmp == null){
                break;
            }
            System.out.println("id:"+curEmp.getId()+"name:\t"+curEmp.getName());
            curEmp = curEmp.next;
        }
        System.out.println();
    }

    public Emp findEmpById(int id){
        if (head == null){
            System.out.println("链表为空");
            return null;
        }
        //辅助指针
        Emp curEmp = head;
        while (true){
            if (curEmp.getId() == id){
                break;
            }
            if (curEmp.next == null){//说明该表中没有该雇员
                curEmp = null;
            }
            curEmp = curEmp.next;
        }
        String s;
        return curEmp;
    }


    /**
     *
     */
    static class  HashTable{
        private EmpLinkedList[] empLinkedListsArray;
        public int size;//表示有多少条链表
        //构造器

        public HashTable(int size) {
            empLinkedListsArray = new EmpLinkedList[size];
            //注意

        }

        //添加雇员
        public void add(Emp emp){
            //根据员工ID添加员工信息,得到该员工应当添加到哪条链表
            int empLinkedLIstNO = hasFun(emp.getId());
            //将emp 添加到对应的链表中
            empLinkedListsArray[empLinkedLIstNO].add(emp);
        }

        public void list(){
            for (int i = 0; i < size; i++){
                empLinkedListsArray[i].list();
            }
        }

        //编写一个散列函数
        public int hasFun(int id){
            return id % size;//取模法
        }
    }

}